package com.fastone.ww.generatefile.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fastone.ww.generatefile.exception.ApiException;
import com.fastone.ww.generatefile.response.enums.CodeEnum;
import lombok.Data;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.io.File;
import java.time.LocalDateTime;

import static com.fasterxml.jackson.annotation.JsonInclude.Include.ALWAYS;

@Data
@JsonInclude(ALWAYS)
public class ResponseResult<T> {

	private String code;

	private String message;

	private T data;


	public ResponseResult(ApiException e) {
		this.code = e.getCode();
		this.message = e.getMessage();
	}

	public ResponseResult(T data) {
		this.code = CodeEnum.SUCCESS.getCode();
		this.message = CodeEnum.SUCCESS.getMessage();
		this.data = data;
	}

	public ResponseResult(String errCode, String errMsg, T data) {
		this.code = errCode;
		this.message = errMsg;
		this.data = data;
	}

	public ResponseResult() {
	}

	public static <T> ResponseResult<T> success(T data) {
		return new ResponseResult<>(data);
	}

	public static <T> ResponseResult<T> badRequestError(String message) {
		return new ResponseResult<>(CodeEnum.BAD_REQUEST_CODE.getCode(), message, null);
	}


	/**
	 * 返回异常
	 *
	 * @param e Exception
	 * @return ResponseResult
	 */
	public static <T> ResponseResult<T> fail(Throwable e) {
		if (e instanceof ApiException) {
			return new ResponseResult<>((ApiException) e);
		}
		return new ResponseResult<>(CodeEnum.SYSTEM_ERROR_CODE.getCode(), e.getMessage(), null);
	}

	public static ResponseResult fail(ApiException e, Object data) {
		return new ResponseResult<>(e.getCode(), e.getMessage(), data);
	}

	/**
	 * 直接返回文件流
	 *
	 * @param file
	 * @return
	 */
	public static ResponseEntity<FileSystemResource> export(File file) {
		if (file == null) {
			return null;
		}
		HttpHeaders headers = new HttpHeaders();
		headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
		headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName());
		headers.add("Pragma", "no-cache");
		headers.add("Expires", "0");
		headers.add("Last-Modified", LocalDateTime.now().toString());
		headers.add("ETag", System.currentTimeMillis() + "");
		return ResponseEntity.ok()
				.headers(headers)
				.contentLength(file.length())
				.contentType(MediaType.APPLICATION_OCTET_STREAM)
				.body(new FileSystemResource(file));
	}

}

